//
//  CNMineViewController.m
//  BeeQuick_19
//
//  Created by clinging on 16/9/24.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNMineViewController.h"
#import "CNUserFeedBackViewController.h"
#import "CNShopViewController.h"
#import "CNNavViewController.h"
#import "CNHelpViewController.h"
#import "CNMineModel.h"
#import "UILabel+CZAddition.h"
#import "CNMineSettingController.h"
#import "CNMineCell.h"
#import "CNShopViewController.h"
#import "CNTopHeaderView.h"
#import "CNHelpViewController.h"
#import "CNMineMessageController.h"
#import "CNUserFeedBackViewController.h"
#import "CNQuestionViewController.h"
#import "CZAdditions.h"
#import "BeeQuick_19-Swift.h"
#import "CNDiscountCouponController.h"
#import "CNComitOrderTableViewController.h"
#import "CNConsigneeAddressTableViewController.h"
#import "ShareSDKManager.h"
#import "CNLoginController.h"
#import <Masonry.h>
#import "CNMyOrderTableViewController.h"



static NSString * reuseIdentifier = @"tttCell";

@interface CNMineViewController () <UITableViewDelegate,UITableViewDataSource,CNTopHeaderViewDelegate,UIGestureRecognizerDelegate>


//定义订单的属性
@property(nonatomic,strong)NSArray * orderMenus;

//定义我的订单的属性
@property(nonatomic,strong)NSArray * mineMenus;
//  定义一个数组
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIView *appView;

@property (nonatomic,strong) NSArray <CNMineModel*> *optionList;

// 用户手机号码
//@property (nonatomic, weak) UILabel *nameLabel;


@end

@implementation CNMineViewController
//NSArray <CNMineModel*>*_optionList;
UILabel *_nameLabel;

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.edgesForExtendedLayout = UIRectEdgeNone;
//    self.extendedLayoutIncludesOpaqueBars = NO;
//    self.modalPresentationCapturesStatusBarAppearance = NO;
    // 设置背景颜色
//    self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
     [self loadData];
    [self loadView];
    [self setupUI];
//    //添加返回按钮
//    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
//                                             
    }
#pragma mark -返回按钮的点击

-(void)setupUI
{
    //设置navigation的返回文字及颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    
    UIImageView *backgroundImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_avatar_bg"]];
    backgroundImg.userInteractionEnabled = YES;
    [self.view addSubview:backgroundImg];
    
    [backgroundImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(self.view.bounds.size.width,150));
        make.centerX;
    }];
    
    //设置按钮
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setImage:[UIImage imageNamed:@"v2_my_settings_icon"] forState:UIControlStateNormal];
    [self.view addSubview:setBtn];
    [setBtn addTarget:self action:@selector(setingClick) forControlEvents:UIControlEventTouchUpInside];
    
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backgroundImg).offset(20);
        make.right.equalTo(backgroundImg).offset(-15);
    }];
    //用户头像
    UIImageView *iconImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_avatar"]];
    iconImage.layer.masksToBounds = YES;
    iconImage.layer.cornerRadius = 35.0;
    iconImage.layer.borderWidth = 1.0;
    iconImage.layer.borderColor = [[UIColor whiteColor] CGColor];
    iconImage.userInteractionEnabled = YES;
    
    //
   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(IconImageClick:)];
    tap.numberOfTapsRequired = 1;
    tap.delegate = self;
    
    [iconImage addGestureRecognizer:tap];
    
    
    
    iconImage.backgroundColor = [UIColor whiteColor];
    [backgroundImg addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 70));
        make.centerY.equalTo(backgroundImg).offset(-10);
        make.centerX.equalTo(backgroundImg);
    }];
    //用户名
    UILabel *nameLabel = [UILabel cz_labelWithText:@"" fontSize:17 color:[UIColor whiteColor]];
    _nameLabel = nameLabel;
    [backgroundImg addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImage.mas_bottom).offset(8);
        make.centerX.equalTo(backgroundImg);
    }];
 
    // 下面的cell
    UITableView *tableView = [UITableView new];
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
//        make.top.equalTo(topv.mas_bottom);
        make.top.equalTo(backgroundImg.mas_bottom);
    }];
    tableView.delegate = self;
    tableView.dataSource = self;

    [tableView registerNib:[UINib nibWithNibName:@"CNMineCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
    
    CNTopHeaderView * topv = [[CNTopHeaderView alloc]initWithFrame:CGRectMake(0, 0, 0, 80)];
    
    topv.delegate = self;
    
    tableView.tableHeaderView = topv;
    
}

#warning - 这面是要添加登录的界面
-(void)IconImageClick:(UITapGestureRecognizer*)recognizer
{
    CNLoginController *loginVC = [[CNLoginController alloc]init];
    [self.navigationController pushViewController:loginVC animated:NO];
    
    loginVC.loginBlock = ^(NSString *str){
        _nameLabel.text = str;
    };

}

//设置按钮点击事件
-(void)setingClick
{
    CNMineSettingController *setting = [[CNMineSettingController alloc]init];
    
    [self.navigationController pushViewController:setting animated:NO];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    NSLog(@"%zd",section);
    switch (section) {
        case 0:
            return 2;
        case 1:
            return 1;
        case 2:
            return 3;
        default:
            return 0;
    }
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CNMineCell *cell = [tableView dequeueReusableCellWithIdentifier: reuseIdentifier forIndexPath:indexPath];
    
//    CNMineModel *model = self.optionList[indexPath.item];
    if (indexPath.section == 0 ) {
    cell.mineModel = _optionList[indexPath.item];
    }
    if(indexPath.section == 1){
  cell.mineModel = _optionList[indexPath.item + 2];
        
    }
    if(indexPath.section == 2){
    cell.mineModel = _optionList[indexPath.item +3 ];
        
    }
    cell.backgroundColor = [UIColor whiteColor];
//
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 4;
    
}
- (void)jumpToController:(UIButton *)sender;
{
    if (sender.tag == kMyOrderBtn) {
              NSLog(@"跳转到我的订单");
        CNMyOrderTableViewController * myOreder = [[CNMyOrderTableViewController alloc]init];
        
        self.navigationController.navigationBarHidden = NO;
        
       [self.navigationController pushViewController:myOreder animated:YES];
        
        
    }
    if (sender.tag == kcouponBtn) {
        //        NSLog(@"跳转到优惠券");
          CNDiscountCouponController * discount = [[CNDiscountCouponController alloc]init];
        self.navigationController.navigationBarHidden = NO;

        [self.navigationController pushViewController:discount animated:YES];
    }
    if (sender.tag == kMyNewsBtn) {
        //        NSLog(@"跳转到我的消息");
        CNMineMessageController * messageVC = [[CNMineMessageController alloc]init];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:messageVC animated:YES];
        
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        //push 到我的收获地址
        CNConsigneeAddressTableViewController * address = [[CNConsigneeAddressTableViewController alloc]init];
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:address animated:NO];
                }
    if (indexPath.section == 0 && indexPath.row == 1)
    {
        //        NSLog(@"跳转到我的店铺");
        CNShopViewController *shop = [[CNShopViewController alloc]init];
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:shop animated:NO];
        
        
    }
    if (indexPath.section == 1 && indexPath.row == 0)
    {
                NSLog(@"跳转到分享");
        [ShareSDKManager shareSDK];
        
        }
    if (indexPath.section == 2 && indexPath.row == 0)
    {
        CNHelpViewController *help = [[CNHelpViewController alloc]init];
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:help animated:YES];
        //        NSLog(@"跳转到客服帮助");
    }
    if (indexPath.section == 2 && indexPath.row == 1)
    {
      //意见反馈
        CNUserFeedBackViewController * user = [[CNUserFeedBackViewController alloc]init];
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:user animated:YES];
    }
    if (indexPath.section == 2 && indexPath.row == 2){
        // 我的二维码
        //调用二维码的方法
        //#import "BeeQuick_19-Swift.h"
            GeneratorQRCodeVC *vc = [[UIStoryboard storyboardWithName:@"GeneratorQRCodeVC" bundle:nil] instantiateInitialViewController];
        
            [vc creatQRCode:@"" imageName:@"v2_title_bar_icon"];
          [vc setViewInfoWithName:@"张三" country:@"北京" headerImageName:@"erha"];
        
            [self.navigationController pushViewController:vc animated:true];
       
    }
  
}
-(void)loadData
{
    NSURL *ulrStr = [[NSBundle mainBundle] URLForResource:@"Mine.plist" withExtension:nil];
    NSArray *temparray = [NSArray arrayWithContentsOfURL:ulrStr];
    
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dict in temparray) {
        CNMineModel * model = [CNMineModel minModelWithDict:dict];
        [array addObject:model];
    }
    self.optionList = array.copy;
    
    [self.tableView reloadData];
//    NSLog(@"%zd",self.optionList.count);
}
@end
